package com.baikeyang.app.controller.api;

import com.baikeyang.common.model.AdministrativeArea;
import com.baikeyang.service.administrativeArea.IAdministrativeAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
public class AdministrativeAreaController {

    @Autowired
    private IAdministrativeAreaService administrativeAreaService;

    /**
     * 查询所有的省/直辖州/直辖市
     */
    @RequestMapping(value = "provinces", method = RequestMethod.GET)
    public List<AdministrativeArea> provinces() {
        return administrativeAreaService.getProvinces();
    };

    /**
     * 根据省份编码查询该省份的城市信息
     */
    @RequestMapping(value = "cities/{provinceCode}", method = RequestMethod.GET)
    public List<AdministrativeArea> cities(@PathVariable String provinceCode) {
        return administrativeAreaService.getCitys(provinceCode);
    };

    /**
     * 根据城市编码查询该城市的区县信息
     */
    @RequestMapping(value = "districts/{cityCode}", method = RequestMethod.GET)
    public List<AdministrativeArea> districts(@PathVariable String cityCode) {
        return administrativeAreaService.getDistricts(cityCode);
    };



    public void saveProince(){
//        System.out.println("Http Web request……");
//        String url = "http://bmfw.www.gov.cn/ZFW-AccessPlatform/front/administrativedivision/cxdqyi.do";
//        url = "http://bmfw.www.gov.cn/ZFW-AccessPlatform/front/administrativedivision/cxdqer.do";//?parentcode=110000
//        //url = "http://bmfw.www.gov.cn/ZFW-AccessPlatform/front/administrativedivision/xiangqcode.do";//?code=110000
//
////    //GET请求
////    String content = HttpUtil.get(url);
//
//        QueryWrapper<Provinces> wrapper = new QueryWrapper<Provinces>();
//        wrapper.eq("level", 2);
//        List<Provinces> provincesList = provincesService.list(wrapper);
//        System.out.println(provincesList);
//
//        for(Provinces provinces1 : provincesList){
//            //POST请求
//            HashMap<String, Object> paramMap = new HashMap<>();
//            paramMap.put("parentcode", provinces1.getCode());
//            String content = HttpUtil.post(url, paramMap);
//
//            JSONObject result = JSONUtil.parseObj(content);
//            JSONObject params = JSONUtil.parseObj(result.get("params").toString());
//            JSONArray areacodes = JSONUtil.parseArray(params.get("resultcode").toString());
//            Provinces p = null;
//            for(Object object : areacodes ){
//                JSONObject jsonObject = JSONUtil.parseObj(object);
//                p = new Provinces();
//                p.setId(new Integer(jsonObject.get("iid").toString()));
//                p.setCode(jsonObject.get("code").toString());
//                p.setParentCode(jsonObject.get("parentcode").toString());
//                p.setLevel(new Integer(jsonObject.get("level").toString()));
//                p.setName(jsonObject.get("name").toString());
//                p.setShortName(jsonObject.get("shortname").toString());
//                p.setFullName(jsonObject.get("fullname").toString());
//                provincesService.save(p);
//                //provincesService.insert(p);
//            }
//
//
//        }




    }


    @RequestMapping(value = "provincesX", method = RequestMethod.GET)
    public void saveProinceXiangQ() {
//        System.out.println("Http Web request……");
//        String url = "http://bmfw.www.gov.cn/ZFW-AccessPlatform/front/administrativedivision/xiangqcode.do";//?code=110000
//        QueryWrapper<Provinces> wrapper = new QueryWrapper<Provinces>();
//        wrapper.eq("level", 3);
//        List<String> ids = new ArrayList<String>();
//        ids.add("2541");
//        ids.add("1966");
//        wrapper.in("id", ids);
//        List<Provinces> provincesList = provincesService.list(wrapper);
//        Provinces p = null;
//        int count = 0;
//        for (Provinces provinces1 : provincesList) {
//            //POST请求
//            HashMap<String, Object> paramMap = new HashMap<>();
//            paramMap.put("code", provinces1.getCode());
//            String content = HttpUtil.post(url, paramMap);
//
//            try {
//
//                JSONObject result = JSONUtil.parseObj(content);
//                JSONObject params = JSONUtil.parseObj(result.get("params").toString());
//                if(StringUtils.isNotEmpty(params.get("resultcode").toString())){
//                    JSONObject resultcode = JSONUtil.parseObj(params.get("resultcode").toString());
//                    p = provinces1;
//                    String sh = resultcode.get("shenghui").toString();//(中华人民共和国首都)
//                    String sp = resultcode.get("shengPopulation").toString();//1300
//                    String sa =resultcode.get("shengArea").toString();//16412
//                    String st = resultcode.get("statistic").toString();//市辖区16
////        p.setArea(sa);
////        p.setPopulation(sp);
//
//                    String area = "", population = "", areaCode = "", post = "";
//                    if(StringUtils.isNotEmpty(resultcode.get("list").toString())){
//                        JSONArray jsonList = JSONUtil.parseArray(resultcode.get("list").toString());
//                        if(CollectionUtil.isNotEmpty(jsonList)){
//                            for (Object object : jsonList) {
//
//                                JSONObject jsonObject = JSONUtil.parseObj(object);
//                                if(jsonObject.get("code").toString().equals(p.getCode())){
//                                    area = jsonObject.get("area").toString();
//                                    population = jsonObject.get("population").toString();
//                                    areaCode = jsonObject.get("areaCode").toString();
//                                    post = jsonObject.get("post").toString();
//                                }
//
//                            }
//                        }
//                    }
//                    p.setPost(post);
//                    p.setAreaCode(areaCode);
//                    p.setPopulation(population);
//                    p.setArea(area);
//                    p.setProvincialCapital(sh);
//                    p.setProvincialPopulation(sp);
//                    p.setProvincialArea(sa);
//                    p.setStatistic(st);
//                    p.setCreateTime(new Date());
//                    p.setCreateUser(0);
//                    p.setUpdateTime(new Date());
//                    p.setUpdateUser(0);
//                    provincesService.updateById(p);
////          count ++;
////          if(count%20== 0){
////            System.out.println("缓存5秒^_^");
////            try {
//                    Thread.sleep(1000L);
////            }catch (Exception e) {
////              e.printStackTrace();
////            }
////            System.out.println("继续执行下一个");
////          }
//
//
//                }
//
//
//            }catch (Exception e) {
//                e.printStackTrace();
//            } finally {
//                System.out.println(content);
//            }
//
//
//        }
    }


}
